package com.bestcem.xm.common.core.sql;

import com.bestcem.xm.common.core.constant.Constants;
import com.bestcem.xm.common.core.uitls.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 类型转换
 * @date 2021-05-25
 */
@Slf4j
public class ConditionDataConverter {
    public static final String DATETIME_FORMAT1 = "yyyy-MM-dd HH:mm:ss";
    public static final String DATETIME_FORMAT2 = "yyyy-MM-dd HH:mm:ss.SSS";
    public static final String DATETIME_FORMAT3 = "yyyy/MM/dd HH:mm:ss";
    public static final String DATETIME_FORMAT4 = "yyyy/MM/dd HH:mm:ss.SSS";
    public static final String DATE_FORMAT1 = "yyyy-MM-dd";
    public static final String DATE_FORMAT2 = "yyyy/MM/dd";

    /**
     * 将字符串转换为日期
     *
     * @param dateStr
     * @return
     */
    public static Date convertToDate(String dateStr) {
        return ConditionDataConverter.convertToDate(dateStr, null);
    }

    /**
     * 将字符串转换为日期
     *
     * @param dateStr
     * @return
     */
    public static Date convertToDate(String dateStr, TimeZone timeZone) {
        dateStr = StringUtils.trimToNull(dateStr);
        if (dateStr == null) {
            return null;
        }

        String dateFormat;
        if (dateStr.contains(Constants.DOT)) {
            if (dateStr.contains(Constants.HYPHEN)) {
                dateFormat = DATETIME_FORMAT2;
            } else {
                dateFormat = DATETIME_FORMAT4;
            }
        } else if (dateStr.contains(Constants.SPACE)) {
            if (dateStr.contains(Constants.HYPHEN)) {
                dateFormat = DATETIME_FORMAT1;
            } else {
                dateFormat = DATETIME_FORMAT3;
            }
        } else {
            if (dateStr.contains(Constants.HYPHEN)) {
                dateFormat = DATE_FORMAT1;
            } else {
                dateFormat = DATE_FORMAT2;
            }
        }

        try {
            SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
            if (timeZone != null) {
                formatter.setTimeZone(timeZone);
            }
            return formatter.parse(dateStr);
        } catch (ParseException e) {
            String msg = String.format("date format illegal, date: %s", dateStr);
            throw new ConditionGrammarException(msg, e);
        }
    }

    /**
     * 将字符串装换为数字
     *
     * @param numberStr
     * @return
     */
    public static Double convertToDouble(String numberStr) {
        numberStr = StringUtils.trimToNull(numberStr);
        if (numberStr == null) {
            return null;
        }

        try {
            return Double.valueOf(numberStr);
        } catch (NumberFormatException e) {
            String msg = String.format("number format illegal, number: %s", numberStr);
            throw new ConditionGrammarException(msg, e);
        }
    }

    /**
     * 将字符串装换为数字
     *
     * @param numberStr
     * @return
     */
    public static Integer convertToInteger(String numberStr) {
        numberStr = StringUtils.trimToNull(numberStr);
        if (numberStr == null) {
            return null;
        }

        try {
            return Integer.valueOf(numberStr);
        } catch (NumberFormatException e) {
            String msg = String.format("number format illegal, number: %s", numberStr);
            throw new ConditionGrammarException(msg, e);
        }
    }
	
	/**
     * 转换为字符串
     *
     * @param obj
     * @param offset
     * @return
     */
    public static String convertToString(Object obj, ZoneOffset offset) {
        if (obj == null) {
            return null;
        }

        if (obj instanceof Date) {
            Date dateObj = (Date) obj;
            LocalDateTime localDateObj = dateObj.toInstant().atOffset(offset).toLocalDateTime();
            return DateUtil.formatString(localDateObj, DATETIME_FORMAT2);
        }

        return obj.toString();
    }

    /**
     * 将字符串转换为整数，失败则返回null
     *
     * @param numberStr
     * @return
     */
    public static Integer tryConvertToInteger(String numberStr) {
        numberStr = StringUtils.trimToNull(numberStr);
        if (numberStr == null) {
            return null;
        }

        Integer result = null;
        try {
            result = Integer.valueOf(numberStr);
        } catch (NumberFormatException e) {
            log.error("number format illegal, numberStr: {}", numberStr, e);
        }
        return result;
    }
}
